package com.letoken.platform.repo.utlis;

import com.letoken.platform.pub.enums.LetokenTokenTypeEnum;
import com.letoken.platform.pub.enums.TokenTransactionStatusEnum;
import com.letoken.platform.pub.enums.TokenTransactionTypeEnum;
import com.letoken.platform.pub.exception.ApiServiceException;
import com.letoken.platform.repo.admin.dao.AdminGasConfigMapper;
import com.letoken.platform.repo.admin.po.AdminGasConfig;
import com.letoken.platform.repo.app.dao.AppUserWalletMapper;
import com.letoken.platform.repo.app.manager.AppUserWalletManager;
import com.letoken.platform.repo.app.manager.TokenManager;
import com.letoken.platform.repo.app.po.AppUserAssetsHistory;
import com.letoken.platform.repo.app.po.AppUserWallet;
import com.letoken.platform.repo.app.po.Token;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

@Component
public class GasUtils {

    @Resource
    private AdminGasConfigMapper adminGasConfigMapper;

    @Resource
    private TokenManager tokenManager;

    @Resource
    private AppUserWalletMapper appUserWalletMapper;

    @Resource
    private DataSourceTransactionManager dataSourceTransactionManager;

    @Resource
    private TransactionDefinition transactionDefinition;

    /**
     *
     * @param userId
     * @param capGas 手续费
     */
    public void systemGasFee(Integer userId,BigDecimal capGas){
       Integer walletId = appUserWalletMapper.checkoutTokenIdAndUserId(LetokenTokenTypeEnum.CAP.getId(),userId);
       AppUserWallet appUserWallet =  appUserWalletMapper.selectForUpdate(walletId);

       if(appUserWallet.getAvailableBalance().compareTo(capGas)<0){
           throw new ApiServiceException("矿工费不足!");
       }
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);

        AppUserWallet w = new AppUserWallet();
        w.setId(appUserWallet.getId());
        w.setAvailableBalance(appUserWallet.getAvailableBalance().subtract(capGas));
        w.updateById();

        AppUserAssetsHistory assetHistory = new AppUserAssetsHistory();
        assetHistory.setUserId(userId);
        assetHistory.setType(TokenTransactionTypeEnum.CAP_GAS.getValue());
        assetHistory.setStatus(TokenTransactionStatusEnum.COMPLETED.getValue());
        assetHistory.setTokenId(LetokenTokenTypeEnum.CAP.getId());
        assetHistory.setAmount(capGas.negate());
        assetHistory.setBalance(w.getAvailableBalance());
        assetHistory.setCreateTime(LocalDateTime.now());
        assetHistory.setUpdateTime(assetHistory.getCreateTime());
        assetHistory.insert();
        dataSourceTransactionManager.commit(transactionStatus);
    }


    public BigDecimal getFee(BigDecimal capGas,Integer type){
        AdminGasConfig adminGasConfig = adminGasConfigMapper.selectById(1);
        Token token = tokenManager.queryById(4);
        if(type.equals(1)){
            capGas = adminGasConfig.getGasFee().divide(new BigDecimal(token.getUsdPrice()),10, RoundingMode.HALF_UP);
        }else{
            capGas = capGas.divide(new BigDecimal(token.getUsdPrice()),10, RoundingMode.HALF_UP);
        }
        return capGas;
    }
}
